#include <string>

using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> map(128, -1);
        int begin = 0;
        int ans = 0;
        for (int i = 0; i < s.size(); i++) {
            if (map[s[i]] == -1) {
                map[s[i]] = i;
                continue;
            }
            ans = max(ans, i - begin);
            for (int j = begin, len = map[s[i]]; j < len; j++) {
                map[s[j]] = -1;
            }
            begin = map[s[i]] + 1;
            map[s[i]] = i;
        }
        ans = max(ans, (int) s.size() - begin);
        return ans;
    }
};